package com.server.controller;

import com.common.context.BaseContext;
import com.common.result.Result;
import com.pojo.dto.CommentDTO;
import com.pojo.entity.Comment;
import com.pojo.vo.CommentVO;
import com.server.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comment")
@Slf4j
@Api(tags = "文章评论接口")
public class CommentController {
    @Autowired
    CommentService commentService;

    @PostMapping("/getComments")
    @ApiOperation(value = "获取评论列表")
    public Result<List<CommentVO>> getCommentList(Integer articleId){
        List<CommentVO> commentList = commentService.getCommentList(articleId);

        return Result.success(commentList);
    }
    @PostMapping("/insertComment")
    @ApiOperation(value = "添加评论")
    public Result insertComment (@RequestBody CommentDTO commentDTO){
        Comment comment = Comment.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(commentDTO,comment);
        if(commentService.insertComment(comment)!=0){
            return Result.success();
        }else {
            return Result.error("Error inserting Comment");
        }
    }

    //根据文章id查询评论数
    @GetMapping("/commentCount")
    public Result getCommentByArticleId(Integer articleId){
        Integer commentCount = commentService.getCommentCount(articleId);
        return Result.success(commentCount);
    }




}
